package com.juyu.miliao.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.domain.CoinPrice;
import com.juyu.miliao.api.domain.H5PayInfo;
import com.juyu.miliao.api.domain.Vip;
import com.juyu.miliao.api.domain.dto.PreferentialInformationTipsDTO;
import com.juyu.miliao.api.service.CoinPriceService;
import com.juyu.miliao.api.service.H5PayService;
import com.juyu.miliao.api.service.H5PayDetailService;
import com.juyu.miliao.api.service.VipService;
import com.juyu.miliao.api.util.NetUtil;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * H5支付控制器
 *
 * @author xuanqicheng
 * @create 2018-10-4:38 PM
 */
@RestController
@RequestMapping("/h5")
@Slf4j
@CrossOrigin
public class H5PayController {

    @Autowired
    private H5PayService h5PayService;

    @Autowired
    private H5PayDetailService h5PayDetailService;

    @Autowired
    private CoinPriceService coinPriceService;

    @Autowired
    private VipService vipService;

    @GetMapping("/user/{userId}")
    public Object getUserInfo(@PathVariable("userId") long userId) throws  Exception{

        return h5PayDetailService.getUserInfoById(userId);
    }

    /**
     * 获取h5 金币列表
     * @param
     * @return
     */
    @GetMapping("/gold/list/{type}")
    public Object getGoldList(@PathVariable("type") Integer type)throws  Exception{

        JSONObject resultJson = new JSONObject();
        try {
            List<CoinPrice> list = coinPriceService.getCoinPriceListByCoinType(type);

            List<Map<String,Object>> resultList=h5PayDetailService.getGoldList(list);

            if(resultList.size()>0){
                resultJson.put("code",200);
                resultJson.put("message","获取成功");
                resultJson.put("data",resultList);
            }else{
                resultJson.put("code",300);
                resultJson.put("message","获取金币信息失败，请联系管理关");
            }
        }catch (Exception ex){
            log.error("获取h5 金币列表异常：{}",ex);
        }


        return resultJson;
    }

    @GetMapping("/vip/list/{type}")
    public Object getVipList(@PathVariable("type") Integer type)throws  Exception{
        JSONObject resultJson = new JSONObject();

        List<Vip> vipList = vipService.getVipListByType(type);

        List<Map<String,Object>> resultList = h5PayDetailService.getVipList(vipList);

        if(resultList.size()>0){
            resultJson.put("code",200);
            resultJson.put("message","获取Vip列表成功");
            resultJson.put("data",resultList);
        }else{
            resultJson.put("code",300);
            resultJson.put("message","获取Vip列表失败，请联系管理关");
        }

        return resultJson;

    }

    @GetMapping("/vip/list/{type}/{userId}")
    public Object getNewVipList(@PathVariable("type") Integer type,
                                @PathVariable("userId") Long userId)throws  Exception{

        return vipService.getNewVipListByType(userId,type);

    }

    /**
     * h5计费
     * @param h5PayInfo
     */
    @PostMapping("/pay")
    public Object getPay(HttpServletRequest request, @RequestBody @Valid H5PayInfo h5PayInfo,
                         BindingResult bindingResult){

        log.info("请求参数：{}",h5PayInfo);

        if(bindingResult.hasErrors()){
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }
        if(h5PayInfo.getRobotOrderType()==null){
            h5PayInfo.setRobotOrderType(0);
        }
        if(h5PayInfo.getH5OrderType()==null){
            h5PayInfo.setH5OrderType(2);
        }

        String ip = NetUtil.getIpAddress(request);

        h5PayInfo.setIpAddress(ip);

        Object object=h5PayService.getH5Pay(h5PayInfo);

        return object;
    }

    /**
     * 优惠信息提示
     * @return
     */
    @GetMapping("/tips")
    public Object preferentialInformationTips(@Valid PreferentialInformationTipsDTO tipsDTO,BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }
        Object object = h5PayService.getPreferentialInformationTips(tipsDTO);
        return object;
    }

}
